﻿@using Volo.Abp.AspNetCore.Mvc.UI.Layout
@using Volo.Abp.UI.Navigation
@inject IPageLayout PageLayout
@model ApplicationMenu
@foreach (var menuItem in Model.Items)
{
    var elementId = string.IsNullOrEmpty(menuItem.ElementId) ? string.Empty : $"id=\"{menuItem.ElementId}\"";
    var cssClass = string.IsNullOrEmpty(menuItem.CssClass) ? string.Empty : menuItem.CssClass;
    var disabled = menuItem.IsDisabled ? "disabled" : string.Empty;
    var url = string.IsNullOrEmpty(menuItem.Url) ? "#" : Url.IsLocalUrl(menuItem.Url) ? Url.Content(menuItem.Url.EnsureStartsWith('~')) : menuItem.Url;
    var customComponentType = menuItem.GetComponentTypeOrDefault();
    var isActive = menuItem.Name == PageLayout.Content.MenuItemName ? "active" : string.Empty;
    if (menuItem.IsLeaf)
    {
        if (customComponentType != null && typeof(ViewComponent).IsAssignableFrom(customComponentType))
        {
            <li class="nav-item my-1 @cssClass" @Html.Raw(elementId)>
                @(await Component.InvokeAsync(customComponentType))
            </li>
        }
        else if (menuItem.Url != null)
        {
            var useLocalePrefix = menuItem.IsSupportsLocalePrefix();
            <li class="nav-item my-1 @cssClass" @Html.Raw(elementId)>
                <a class="nav-link underline-animate px-3 @disabled @isActive" href="@url" add-culture="useLocalePrefix" target="@menuItem.Target">
                    @if (menuItem.Icon != null)
                    {
                        <i class="@menuItem.Icon"></i>
                    }
                    @menuItem.DisplayName
                </a>
            </li>
        }
    }
    else
    {
        <li class="nav-item my-1">
            <div class="dropdown">
                @if (customComponentType != null && typeof(ViewComponent).IsAssignableFrom(customComponentType))
                {
                    @(await Component.InvokeAsync(customComponentType))
                }
                else
                {
                    <a class="nav-link underline-animate dropdown-toggle px-3 @disabled @isActive" href="#" id="Menu_@(menuItem.Name)" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        @if (menuItem.Icon != null)
                        {
                            <i class="@menuItem.Icon"></i>
                        }
                        @menuItem.DisplayName
                    </a>
                    <div class="dropdown-menu ms-3" aria-labelledby="Menu_@(menuItem.Name)">
                        @foreach (var childMenuItem in menuItem.Items)
                        {
                            @await Html.PartialAsync("Components/Menu/_MenuItem", childMenuItem)
                        }
                    </div>
                }
            </div>
        </li>
    }
}

